/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccesoADatos;

import Logica.Rol;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author LiN
 */
public class DatosRoles {
    public String fileName="RolConfiguration.txt";
    public DatosRoles(){}
    
    public void saveRol(ArrayList array) throws FileNotFoundException, IOException{
        FileOutputStream fileOut=new FileOutputStream(fileName,false);
        Iterator save=array.iterator();
        while(save.hasNext()){
            Rol rol=(Rol) save.next();
            saveRol(rol,fileName);
        }
        fileOut.close();
    }
    public boolean saveRol(Rol rol,String file){
        try
      {
         FileOutputStream fileOut = new FileOutputStream(file,true);
         ObjectOutputStream out =  new ObjectOutputStream(fileOut);
         out.writeObject(rol);
         out.close();
         fileOut.close();
      }catch(IOException i)
      {
          i.printStackTrace();
          return false;
      }

        return true;
    }
    
    public ArrayList<Rol> loadRol() throws FileNotFoundException, IOException, ClassNotFoundException{
        ArrayList<Rol> lista = new ArrayList();
        try {
            Rol s;
            FileInputStream fis = new FileInputStream(fileName);
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                s = (Rol) ois.readObject();

                if (s != null) {
                    lista.add(s);
                }

            }
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(Configuracion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | ClassNotFoundException ex) {
            //Logger.getLogger(Configuracion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lista;
    }
    
}
